<!-- 简单注册 - smsRegister  -->
<template>
  <view>
    <!-- 分享链接提示 -->
    <view v-if="!hasShareInfo" class="share-tip">
      <text class="tip-text">⚠️ 请通过分享链接进入注册</text>
    </view>

    <!-- 表单项 -->
    <uni-forms
      ref="smsRegisterRef"
      v-model="state.model"
      :rules="state.rules"
      validateTrigger="bind"
      labelWidth="140"
      labelAlign="center"
    >
      <uni-forms-item name="mobile" label="手机号">
        <uni-easyinput
          placeholder="请输入手机号"
          v-model="state.model.mobile"
          type="number"
          :inputBorder="false"
        >
        </uni-easyinput>
      </uni-forms-item>

      <uni-forms-item name="password" label="密码">
        <uni-easyinput
          type="password"
          placeholder="请输入密码"
          v-model="state.model.password"
          :inputBorder="false"
        >
        </uni-easyinput>
      </uni-forms-item>
    </uni-forms>

    <!-- 注册按钮 -->
    <view class="action-buttons">
      <button class="ss-reset-button login-btn-start" @tap="smsRegisterSubmit"> 注册 </button>
    </view>
  </view>
</template>

<script setup>
  import { computed, ref, reactive, unref } from 'vue';
  import sheep from '@/sheep';
  import { mobile, password } from '@/sheep/validate/form';
  import { closeAuthModal } from '@/sheep/hooks/useModal';

  const props = defineProps({
    agreeStatus: {
      type: Boolean,
      default: false,
    },
  });

  const smsRegisterRef = ref(null);
  
  const isLogin = computed(() => sheep.$store('user').isLogin);

  const emits = defineEmits(['onConfirm']);

  // 数据
  const state = reactive({
    model: {
      mobile: '', // 手机号
      password: '', // 密码
    },
    rules: {
      mobile,
      password,
    },
  });

  // 检查是否有分享信息
  const hasShareInfo = computed(() => {
    const shareLog = uni.getStorageSync('shareLog');
    console.log("shareLog",shareLog);
    return shareLog && shareLog.shareId && shareLog.shareId !== '0';
  });

  // 简单注册
  async function smsRegisterSubmit() {
    const validate = await unref(smsRegisterRef)
      .validate()
      .catch((error) => {
        console.log('error: ', error);
      });
    if (!validate) return;

    if (!props.agreeStatus) {
      emits('onConfirm',true);
      sheep.$helper.toast('请勾选同意');
      return;
    }

    // 检查是否有分享信息
    if (!hasShareInfo.value) {
      sheep.$helper.toast('请通过分享链接进入注册');
      return;
    }

    const { code } = await sheep.$api.user.smsRegister({
      ...state.model,
      shareInfo: uni.getStorageSync('shareLog') || {},
    });
    if (code === 1) {
      closeAuthModal();
    }
  }
</script>

<style lang="scss" scoped>
  @import '../index.scss';

  .share-tip {
    margin-top: 20rpx;
    text-align: center;
    
    .tip-text {
      color: #ff6b35;
      font-size: 24rpx;
      background: rgba(255, 107, 53, 0.1);
      padding: 10rpx 20rpx;
      border-radius: 20rpx;
      border: 1rpx solid rgba(255, 107, 53, 0.3);
    }
  }
</style>
